package com.example.linearlist;

public class LinkList<E> {

    private Node<E> firstNode;
    private Node<E> lastNode;
    private int length;

    public LinkList() {
        firstNode = null;
        lastNode = null;
        length = 0;
    }

    public void addFirst(E element){
        if(firstNode == null){
            firstNode = new Node<>(element, null, null);
            lastNode = firstNode;
            return;
        }
        Node<E> n = new Node<>(element, null, firstNode);
        firstNode.prev = n;
        firstNode = n;
    }

    public void addLast(E element){
        if(firstNode == null){
            addFirst(element);
            return;
        }
        Node<E> n = new Node<>(element, lastNode, null);
        lastNode.next = n;
        lastNode = n;
    }

    private static class Node<E> {
        E data;
        Node<E> prev;
        Node<E> next;

        public Node(E data, Node<E> prev, Node<E> next) {
            this.data = data;
            this.prev = prev;
            this.next = next;
        }
    }
}
